/**
 * 自定义函数
 * */


//列表
function getList(){
    var uinfo = getUserInfo();
    var formData = {
        role_id: uinfo.role_id, depart_id: uinfo.depart_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'workhour/index',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: '权限错误'});
                return
            }
            getHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//拼接字符串
function getHtml(d){
    var h = '', i, ii, child = [];
    for (i = 0; i < d.length; i ++) {
        h += '<table class="tbox" cellspacing="0">';
        h += '<tr>';
        h += '<td class="tdl">工时名称</td>';
        h += '<td class="tdr">' + d[i].title + '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl" style="vertical-align: top;">工时内容</td>';
        h += '<td class="tdr">';
        child = d[i].cntList;
        for (ii = 0; ii < child.length; ii ++) {
            h += '<div>' + child[ii].title + '</div>';
            h += '<input type="hidden" id="cnt' + child[ii].id + '" value="' + child[ii].title + '"/>';
            h += '<div>';
            h += '<a href="javascript:;" onclick="cntEdit(' + child[ii].id + ');">修改</a>';
            h += ' <a href="javascript:;" style="color: red;" onclick="cntDel(' + child[ii].id + ');">删除</a>';
            h += '</div>';
        }
        h += '</td>';
        h += '</tr>';
        h += '<tr>';
        h += '<td class="tdl"></td>';
        h += '<td class="tdr">';
        h += '<a href="javascript:;" onclick="cntAdd(' + d[i].id + ');">新加内容</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" onclick="getOne(' + d[i].id + ');">修改</a>';
        h += ' &nbsp; ';
        h += '<a href="javascript:;" style="color: red;" onclick="whDel(' + d[i].id + ');">删除</a>';
        // h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
        h += '</td>';
        h += '</tr>';
        h += '</table>';
    }
    $('#tbody').html(h);
}


// 获取详情
function getOne(v){
    var formData = {
        id: v,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'workhour/one',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            editHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//编辑弹窗
function editHtml(e){
    var d = e.data;
    var h = '';
    h += '<div class="popMask"></div>';
    h += '<div class="popCnt">';
    h += '<div>工时名称</div>';
    h += '<input type="text" id="title" placeholder="输入名称" value="' + d.title + '"/>';
    h += '<div class="popBtn">';
    h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
    h += '<button onclick="modify(' + d.id + ');">保存</button>';
    h += '</div>';
    h += '</div>';
    $('#formPop').html(h);
}


////编辑弹窗
//function edit(v){
//    var title = $('#title' + v).val();
//    var h = '';
//    h += '<div class="popMask"></div>';
//    h += '<div class="popCnt">';
//    h += '<div>部门名称</div>';
//    h += '<input type="text" id="title" value="' + title + '"/>';
//    h += '<div class="popBtn">';
//    h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
//    h += '<button onclick="modify(' + v + ');">保存</button>';
//    h += '</div>';
//    h += '</div>';
//    $('#formPop').html(h);
//}


//删除
function whDel(v){
    var formData = {
        id: v, 
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'workhour/del',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                window.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
 }


//编辑保存
function modify(v){
   var title = $('#title').val();
   var formData = {
       id: v, title: title,
   };
   //console.log(formData); return
   $.ajax({
       type: apiMethod,
       url: apiDomain + 'workhour/modify',
       data: formData,
       success: function (res) {
           console.log(res);
           if (res.code < 0) {
               msgPop({msg: res.msg});
               return
           }
           msgPop({msg: '成功'});
           setTimeout(function(){
               window.location.reload();
           }, 500);
       },
       error: function () {
           console.log('网络错误');
       }
   });
}


//内容弹窗
function cntAdd(whid){
    var h = '';
    h += '<div class="popMask"></div>';
    h += '<div class="popCnt">';
    h += '<div>任务内容</div>';
    h += '<input type="text" id="cnt" placeholder="输入内容"/>';
    h += '<div class="popBtn">';
    h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
    h += '<button onclick="setCntAdd(' + whid + ');">保存</button>';
    h += '</div>';
    h += '</div>';
    $('#formPop').html(h);
}


//内容保存
function setCntAdd(v){
   var title = $('#cnt').val();
   var formData = {
       wh_id: v, cnt: title,
   };
   $.ajax({
       type: apiMethod,
       url: apiDomain + 'workhour/cnt/add',
       data: formData,
       success: function (res) {
           console.log(res);
           if (res.code < 0) {
               msgPop({msg: res.msg});
               return
           }
           msgPop({msg: '成功'});
           setTimeout(function(){
               window.location.reload();
           }, 500);
       },
       error: function () {
           console.log('网络错误');
       }
   });
}


//内容弹窗
function cntEdit(v){
    var title = $('#cnt' + v).val();
    var h = '';
    h += '<div class="popMask"></div>';
    h += '<div class="popCnt">';
    h += '<div>任务内容</div>';
    h += '<input type="text" id="cnt" placeholder="输入内容" value="' + title + '"/>';
    h += '<div class="popBtn">';
    h += '<button style="background: orangered;" onclick="$(\'#formPop\').html(\'\');">取消</button> &nbsp;';
    h += '<button onclick="setCntModify(' + v + ');">保存</button>';
    h += '</div>';
    h += '</div>';
    $('#formPop').html(h);
}


//内容保存
function setCntModify(v){
   var title = $('#cnt').val();
   var formData = {
       id: v, cnt: title,
   };
   $.ajax({
       type: apiMethod,
       url: apiDomain + 'workhour/cnt/modify',
       data: formData,
       success: function (res) {
           console.log(res);
           if (res.code < 0) {
               msgPop({msg: res.msg});
               return
           }
           msgPop({msg: '成功'});
           setTimeout(function(){
               window.location.reload();
           }, 500);
       },
       error: function () {
           console.log('网络错误');
       }
   });
}


//内容删除
function cntDel(v){
   var formData = {
       id: v, 
   };
   $.ajax({
       type: apiMethod,
       url: apiDomain + 'workhour/cnt/del',
       data: formData,
       success: function (res) {
           console.log(res);
           if (res.code < 0) {
               msgPop({msg: res.msg});
               return
           }
           msgPop({msg: '成功'});
           setTimeout(function(){
               window.location.reload();
           }, 500);
       },
       error: function () {
           console.log('网络错误');
       }
   });
}